﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Data.Utils;

namespace Data.Repositories
{
    public class NewsletterRepository
    {
        ModeloCantinhoContainer ctx = new ModeloCantinhoContainer();
        
        public IEnumerable<Newsletter> GetAll()
        {
            return ctx.Newsletter;
        }

        public IEnumerable<Newsletter> GetAllRegisto()
        {
            return ctx.Newsletter.Where(x => x.Registo);
        }

        public void Aderir(string tentar, DateTime? family_date, string email)
        {
            MailingList mail = new MailingList();
            mail.Activo = true;
            mail.Email = email;
            mail.Newsletter.Add(GetById(3));

            if (tentar == "1")
            {
                mail.TentarEngravidar = true;
            }
            else
            {
                mail.DataNascimento = family_date.Value;
                mail.DataFormatada = family_date.Value.ToShortDateString();
            }

            ctx.MailingList.AddObject(mail);
            SaveChanges();
            MailTools.EnviaMailNewsletter(email);
        }

        public Newsletter GetById(int id)
        {
            return ctx.Newsletter.Where(x=>x.Id == id).FirstOrDefault();
        }

        public void SaveChanges()
        {
            try
            {
                ctx.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
